package com.heima.cartservice.listener;

import com.heima.cartservice.service.ICartService;
import com.hmall.common.utils.UserContext;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Types;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class RemoveCartListener {
    @Autowired
    private ICartService cartService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "cart.clear.queue",  durable = "true"),
            exchange = @Exchange(name = "trade.topic",type = ExchangeTypes.DIRECT),
            key = "order.create"
    ))
    public void listenerRemoveCart(Collection<Long> orderId, Message message){
        Long userId = message.getMessageProperties().getHeader("userId");
        UserContext.setUser(userId);
        System.out.println("清空购物车");
        System.out.println(orderId);
        cartService.removeByItemIds(orderId);
    }

}
